package com.example.kryssz.myapplication;

import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Message;
import android.os.Handler.Callback;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    double lat = 0;
    double lon = 0;
    float acc = 0;
    String other = "";
    Timer t = new Timer();
    TextView txLat;
    TextView txLon;
    TextView txSome;
    TextView txAcc;
    GPSTracker tracker;// = new GPSTracker(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //t.scheduleAtFixedRate( );
        txLat = (TextView) findViewById(R.id.txLat);
        txLon = (TextView) findViewById(R.id.txLon);
        txAcc = (TextView) findViewById(R.id.txAcc);
        txSome = (TextView) findViewById(R.id.txSome);
        t.scheduleAtFixedRate(new locationTask(), 1000, 1000);
        tracker = new GPSTracker(this);
    }

    private void getLocation() {


        if (tracker.canGetLocation() == false) {

            lat = -2;
            lon = -2;
        } else {
            lat = tracker.getLatitude();
            lon = tracker.getLongitude();


            if(tracker.getlocation() == null)
            {
                other = "null";
            }
            else
            {
                acc = tracker.getlocation().getAccuracy();


                Bundle b = tracker.getlocation().getExtras();
                try
                {
                    int stats = b.getInt("satellites");
                    other = String.valueOf("Satellites:"+stats);
                }
                catch (Exception e)
                {
                    other = String.valueOf("Satellites: unknown");
                }

            }


        }
    }

        public String DoubleToString(double number, int precision)
        {
            NumberFormat nf = new DecimalFormat("#.######");
            return nf.format(number);
        }


        public void onLocationChanged(Location location) {
         /*   this.location = location;
            getLatitude();
            getLongitude();*/
        }
    final Handler h = new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            try {
                getLocation();

                txLat.setText(DoubleToString(lat,6));
                txLon.setText(DoubleToString(lon,6));
                txAcc.setText(String.valueOf(acc));
                txSome.setText(other);
            }
            catch (Exception e)
            {
                txLat.setText("Exception");
                txLon.setText(String.valueOf(e.getMessage()));
            }
            return false;
        }
    });

    class locationTask extends TimerTask {

        @Override
        public void run() {

            h.sendEmptyMessage(0);
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
